package com.ygg.controller.api;

import com.ygg.helper.HttpRequestHelper;
import com.ygg.model.hosp.Department;
import com.ygg.model.hosp.Hospital;
import com.ygg.model.hosp.Schedule;
import com.ygg.result.Result;
import com.ygg.service.DepartmentService;
import com.ygg.service.HospitalService;
import com.ygg.service.HospitalSetService;
import com.ygg.service.ScheduleService;
import com.ygg.vo.hosp.DepartmentQueryVo;
import com.ygg.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Resource
    private HospitalService hospitalService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    /**
     * 查询医院
     *
     * @param request
     * @return
     */
    @PostMapping("/hospital/show")
    public Result<Hospital> show(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = map.get("hoscode").toString();

        Hospital hospital = hospitalService.getByHosCode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 验签方法
     *
     * @param map 参数
     * @return 医院唯一code
     */
//    private String isSign(Map<String, Object> map) {
//        String hoscode = (String) map.get("hoscode");
//
//        if (StringUtils.isEmpty(hoscode)) {
//            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
//        }
//        //获取传过来的医院签名
//        String sign = (String) map.get("sign");
//
//        //查询数据库加密后进行签名比对
//        String singKey = hospitalSetService.getSign(hoscode);
//        String singKeyMD5 = MD5.encrypt(singKey);
//
//        if (!sign.equals(singKeyMD5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
//        return hoscode;
//    }

    /**
     * 添加医院
     *
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result<String> saveHospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);


        //传输过程中"+"转换成了" " 所以我们要转换过来
        String logoData = map.get("logoData").toString();
        String s = logoData.replaceAll(" ", "+");
        map.put("logoData", s);

        hospitalService.save(map);
        return Result.ok();
    }

    /**
     * 添加科室
     *
     * @param request
     * @return
     */
    @PostMapping("saveDepartment")
    public Result<String> saveDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        departmentService.save(map);
        return Result.ok();
    }

    /**
     * 科室分页查询
     *
     * @param request
     * @return
     */
    @PostMapping("/department/list")
    public Result<Page<Department>> list(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        Object page = map.get("page");
        Object limit = map.get("limit");
        Integer pageNum = StringUtils.isEmpty(page) ? 1 : Integer.parseInt(page.toString());
        Integer pageSize = StringUtils.isEmpty(limit) ? 1 : Integer.parseInt(limit.toString());

        DepartmentQueryVo vo = new DepartmentQueryVo();
        vo.setHoscode(map.get("hoscode").toString());
        Page<Department> pageDepartment = departmentService.findPageDepartment(pageNum, pageSize, vo);

        return Result.ok(pageDepartment);
    }

    /***
     * 删除科室
     * @param request
     * @return
     */
    @PostMapping("/department/remove")
    public Result<String> remove(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);


        departmentService.removeDepartment(map);

        return Result.ok();
    }

    /**
     * 添加排班
     * @param request
     * @return
     */
    @PostMapping("/saveSchedule")
    public Result<String> saveSchedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);


        scheduleService.saveSchedule(map);

        return Result.ok();
    }

    @PostMapping("/schedule/list")
    public Result<Page<Schedule>> scheduleList(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);


        Object page = map.get("page");
        Object limit = map.get("limit");
        Integer pageNum = StringUtils.isEmpty(page) ? 1 : Integer.parseInt(page.toString());
        Integer pageSize = StringUtils.isEmpty(limit) ? 1 : Integer.parseInt(limit.toString());

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(map.get("hoscode").toString());

        Page<Schedule> pageSchedule = scheduleService.scheduleList(pageNum,pageSize,scheduleQueryVo);

        return Result.ok(pageSchedule);
    }

    @PostMapping("/schedule/remove")
    public Result<String> scheduleRemove(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);


        scheduleService.scheduleRemove(map);

        return Result.ok();

    }
}

